<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<h1 style="color: red;">欢迎进入注册页面</h1>
<form action="/regist" name="myform" method="post" onsubmit="return checkAll()">
    请填写您的用户名:<input type="text" name="registName"><br><br>
    请填写您的密码 : <input type="password" name="registPasswd" onclick="checkUsername()"><br><br>
    请再输入您的密码:<input type="password" name="registPasswd2" onclick="checkPasswd()"><br><br>
    <input type="submit" value="注册" onclick="checkPasswd2()">
</form>
</body>
<script>
    function checkUsername(){
        var username = document.myform.registName.value;
        if(username.match(/\w{8,16}/)==null){ //可以写成/[0-9A-Za-z_]{8,16}/
            alert("请输入8-16位的账号");
            return false;
        }  else{
            return true;
        }
    }
    function checkPasswd(){
        var passwd = document.myform.registPasswd.value;
        if(passwd.match(/\w{8,16}/)==null){ //可以写成/[0-9A-Za-z_]{8,16}/
            alert("请输入8-16位的密码");
            return false;
        }  else{
            return true;
        }
    }

    function checkPasswd2(){
        var passwd = document.myform.registPasswd.value;
        var passwd2 = document.myform.registPasswd2.value;
        if(passwd!==passwd2 && passwd == null){
            alert("表单提示:请输入相同密码")
            return false;
        }else{
            return true;
        }
    }

    function checkAll(){
        return checkUsername() && checkPasswd() && checkPasswd2();
    }

</script>
</html>